/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package music;

import MidiGui.MusicFonts;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import music.types.NoteTypes;

/**
 *
 * @author TJ
 */
public class TimeSignature {
    public static TimeSignature COMMON_TIME=new TimeSignature(4,NoteTypes.QUARTER,true);
    public static TimeSignature CUT_TIME=new TimeSignature(2,NoteTypes.HALF,true);
    private int num;
    private NoteTypes denom;
    private boolean c_time=false;
    public TimeSignature(int numerator,NoteTypes denominator,boolean c) {
        c_time=c;
        num=numerator;
        denom=denominator;
    }
    public TimeSignature(int numerator,NoteTypes denominator) {
        num=numerator;
        denom=denominator;
    }
    public int getNumerator(){return num;}
    public int getDenominator(){return denom.getTime();}
    public NoteTypes getBeatType(){return denom;}
    public static boolean validateTimeSignature(TimeSignature t) {
        return log2(t.getDenominator()) && t.getDenominator()<Score.MAX_DIVISION && t.getNumerator()<=100;
    }
    private static boolean log2(int num) {
        int tmp=1;
        while(tmp<num) {
            tmp=tmp+tmp;
        } 
        return tmp==num;
    }
    public boolean equals(Object o) {
        if(o instanceof TimeSignature) {
            return ((TimeSignature)o).getNumerator()==num && this.getDenominator()==((TimeSignature)o).getDenominator(); 
        }
        return false;
    }
    public int getTimeWidth(SpacingOptions s){
        Font font=MusicFonts.getFont("OpusRegular").deriveFont((float)(s.getLineSpacing()*4+1));
        FontMetrics m=Toolkit.getDefaultToolkit().getFontMetrics(font);
        return Math.max(getTimeWidth(m,this.getNumerator()),
                 getTimeWidth(m,this.getDenominator())
                         );
    }
    private int getTimeWidth(FontMetrics m,int number) {
        String render="";
       int width=0;
       while(number>0) {
           char c=(char)(0xf030+number%10);
           render=c+render;
           width+=m.charWidth(c);
           number/=10;
       }
       return width;
    }
    //public void
    public int renderTime(Graphics g,int x, int bottomLine,SpacingOptions s) {
            Font font=MusicFonts.getFont("OpusRegular").deriveFont((float)(s.getLineSpacing()*4+1));
             g.setFont(font); 
             FontMetrics m=Toolkit.getDefaultToolkit().getFontMetrics(font);
            if(this==TimeSignature.COMMON_TIME) {
                  g.drawString(""+((char)0xf063), x, bottomLine-2*s.getLineSpacing());
                  return m.charWidth((char)0xf063);
             }else if(this==TimeSignature.CUT_TIME){
                 g.drawString(""+((char)0xf043), x, bottomLine-2*s.getLineSpacing());
                 return m.charWidth((char)0xf043);
             }else{
                 return Math.max(renderInt(g,m,this.getNumerator(), x, bottomLine-3*s.getLineSpacing()),
                 renderInt(g,m,this.getDenominator(), x, bottomLine-s.getLineSpacing())
                         );
             }
   }
   private int renderInt(Graphics g,FontMetrics m, int number, int x, int y){
       String render="";
       int width=0;
       while(number>0) {
           char c=(char)(0xf030+number%10);
           render=c+render;
           width+=m.charWidth(c);
       }
       g.drawString(render, x, y);
       return width;
   }
}
